﻿var flowId = "DATA_SUBMIT_FLOW";
var fromEleIds = "#nodeName,#userCodeBut";
var isModify;//是否修改
$(initPage);
function initPage() {
	//新增
	$("#btnAdd").click(function() {
		$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled",false);
		$(fromEleIds).attr("disabled",false);
		isModify = false;
		clearValues();
	});
	
	//修改
	$("#btnEdit").click(function() {
		$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled",false);
		$(fromEleIds).attr("disabled",false);
		isModify = true;
	});
	
	//取消
	$("#btnCancle").click(function() {
		$("#btnEdit,#btnDelete,#btnSave,#btnCancle").attr("disabled", true);
		$("#btnAdd").attr("disabled",false);
		$(fromEleIds).attr("disabled", true);
		clearValues();
	});
	
	//保存
	$("#btnSave").click(function() {
		saveInfo();
	});
	
	//删除
	$("#btnDelete").click(function() {
		deleteInfo();
	});
	
	$("#userCodeBut").click(function(){
		var retVal = window.showModalDialog(BASE_PATH + "page/maintain/user_select.jsp","","dialogWidth:500px;dialogHeight:280px;help:no;center:yes;status:no");
		if (retVal) {
			$("#userCode").val(retVal)
		}
	});
	
	queryList();
}

//清除表单值
function clearValues() {
	$("#nodeName,#userCode").val("");
	$("tr","#data_list").attr("bgColor","#FFFFFF");
}

//保存
function saveInfo() {
	if($.validInput("nodeName", "步骤名称", true, "\<\>\'\"", 50)){
		return false;
	}
	if($.validInput("userCode", "审核人", true, "\<\>\'\"", 30)){
		return false;
	}

	var action = isModify ? "modify" : "add";
	$.post("m/submit_flow/" + action, {
			flowId:flowId,
			nodeId:$.trim($("#nodeId").val()),
			nodeName:$.trim($("#nodeName").val()),
			userCode:$.trim($("#userCode").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				alert("系统提示：保存成功！");
				$("#btnCancle").click();
				queryList();
			} 
		}, "json");
}

//删除
function deleteInfo() {
	if (!confirm("确定要删除记录吗？")) {
		return;
	}
	$.post("m/submit_flow/delete", {
			nodeId:$.trim($("#nodeId").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				$("#btnCancle").click();
				queryList();
			} 
		}, "json");
}

function queryList() {
	$.post("m/submit_flow/query", {
		flowId: flowId
	}, function(data) {
		if ($.checkErrorMsg(data) ) {
			var fileList = "";
			if(data.flowNodes){
				var dataList = data.flowNodes;
				$(dataList).each(function(i) {
					var assignValue =  dataList[i].nodeAssigns[0].assignValue;
					fileList += "<tr class='table_td_font' bgColor='#ffffff' onclick='selectTR(this)' nodeid='" + dataList[i].nodeId + "'>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + (i + 1) + "</td>";
					fileList += "<td align='left'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].nodeName + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + assignValue + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>";
					if (dataList.length > 1) {
						if (i == 0) {
							fileList += "<span style='width:20px'/> <img src='themes/comm/images/sortDown.gif' onclick=\"sortObj('" + dataList[i].nodeId + "', false)\" title='向下'>";
						} else if (i == dataList.length - 1) {
							fileList += "<img src='themes/comm/images/sortUp.gif' onclick=\"sortObj('" + dataList[i].nodeId + "', true)\" title='向上'> <span style='width:15px'/>";
						} else {
							fileList += "<img src='themes/comm/images/sortUp.gif' onclick=\"sortObj('" + dataList[i].nodeId + "', true)\" title='向上'> <img src='themes/comm/images/sortDown.gif' onclick=\"sortObj('" + dataList[i].nodeId + "', false)\" title='向下'>";
						}
					}
					fileList += "</td></tr>";
				});
			}
			$("#data_list").html(fileList);
		} 
	}, "json");
}

function selectTR(obj) {
	$(obj).attr("bgColor","#FAEBD7").siblings().attr("bgColor","#FFFFFF");
	$("#nodeId").val($(obj).attr("nodeid"));
	var tds = $(obj).find("td");
	$("#nodeName").val(tds.eq(1).text());
	$("#userCode").val(tds.eq(2).text());
	$("#btnSave,#btnCancle").attr("disabled", true);
	$("#btnAdd,#btnEdit,#btnDelete").attr("disabled",false);
	$(fromEleIds).attr("disabled", true);
}

function  sortObj(nodeId, flag) {
	$.post("m/submit_flow/sort", {
			nodeId:nodeId,
			sortUp:flag
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				$("#btnCancle").click();
				$("#data_list").html("");
				queryList();
			} 
		}, "json");
	event.cancelBubble=true;
}